<?php

namespace app\admin\model\kuerp;

use think\Model;

class CollectionSale extends Model
{
    // 表名
    protected $name = 'kuerp_collection_sale';
    // 追加属性
    protected $append = [
        'purchase_time_text',
    ];

    public function getPurchaseTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['purchase_time']) ? $data['purchase_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function collection()
    {
        return $this->belongsTo('Collection', 'collection_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goodssales()
    {
        return $this->belongsTo('GoodsSales', 'purchase_number', 'purchase_code', [], 'LEFT')
            ->field('id,purchase_code,purchase_time,money_tax_price,back_money,all_money,pay_money,serials,lock_code,buy_type,buy_version,note,createtime')->setEagerlyType(0);
    }

    public function goodssalesinfo()
    {
        return $this->belongsTo('GoodsSalesInfo', 'purchase_number', 'purchase_code', [], 'LEFT')
            ->field('purchase_code,goods_code,goods_type,number,unit_price,back_money,all_money,remark,period,sale_user_name')->setEagerlyType(0);
    }

}